<?php
/**
 * 后台遮罩管理
 *
 * 后台遮罩管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\MasksService\MasksBackService as MaskService;

class MaskController extends Controller
{

    protected $maskService;
    public function __construct(MaskService $maskService)
    {
        $this->maskService = $maskService;
    }

    /**
     * 获取蒙版列表
     * @param Request $request
     * @return $this
     */
    public function getList(){
        $masks = $this->maskService->getList(15);
        return view('backstage.masks.list')
            ->with('masks',$masks);
    }

    /**
     * 添加蒙版页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        return view('backstage.masks.add');
    }

    /**
     * 添加蒙版
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'original_image' => 'required|image|max:500',
            'mask_preview_image' => 'required|image|max:500'
        ]);
        $data = $request->all();
        unset($data['_token']);
        $flag = $this->maskService->add_mask($data);
        return $flag ? redirect()->back()->with('success_message', '添加成功') : redirect()->back()->with('error_message', '添加失败');
    }

    /**
     * 更新蒙版页面
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function getUpdate($mask_id){
        $mask_id = intval($mask_id);
        //获取类型详细信息
        $mask = $this->maskService->get_mask_by_id($mask_id);
        if(empty($mask)){
            return redirect()->back()->with('error_message', '未找到该遮罩');
        }
        return view('backstage.masks.update')
            ->with('mask',$mask);
    }

    /**
     * 更新蒙版
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request,[
            'mask_id'=>'required|alpha_dash|exists:masks,mask_id,state,1',
            'mask_image' => 'image|max:500',
            'mask_preview' => 'image|max:500'
        ]);
        $data = $request->all();
        unset($data['_token']);
        $flag = $this->maskService->update_mask($data);
        return $flag ? redirect()->back()->with('success_message', '修改成功') : redirect()->back()->with('error_message', '修改失败');
    }

    /**
     * 删除蒙版
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($mask_id){
        $mask_id = intval($mask_id);
        $mask = $this->maskService->get_mask_by_id($mask_id);
        if(empty($mask)){
            return redirect()->back()->with('success_message','删除成功');
        }
        $flag = $this->maskService->remove_mask($mask_id);
        return $flag ? redirect()->back()->with('success_message', '删除成功') : redirect()->back()->with('error_message', '删除失败');
    }
}
